Imports CapaDeDatos
Public Class nNotaDeDebito
    Private Cliente As CapaDeNegocios.nCliente = New CapaDeNegocios.nCliente
    Private Localidad As CapaDeNegocios.nLocalidad = New CapaDeNegocios.nLocalidad
    Private CategoriaFiscal As CapaDeNegocios.nCategoriaFiscal = New CapaDeNegocios.nCategoriaFiscal
    Private DetalleFactura As CapaDeNegocios.nDetalleFacturaVenta = New CapaDeNegocios.nDetalleFacturaVenta
    Private Factura As dFactura = New dFactura()

    Private NotaDeDebito As dNotaDeDebito = New dNotaDeDebito()
    Public Function Insertar(ByVal Numero As String, ByVal IdTalonario As String, ByVal IdFactura As String, ByVal Total As String, ByVal Concepto As String) As Boolean
        Return NotaDeDebito.Insertar(Numero, IdTalonario, IdFactura, Total, Concepto)
    End Function
    Public Function Eliminar(ByVal idNotaDeDebito As String) As Boolean
        Return NotaDeDebito.Eliminar(idNotaDeDebito)
    End Function
    Public Function BuscarFiltrado(ByVal idNotaDeDebito As String, ByVal Numero As String, ByVal Fecha As String, ByVal IdTalonario As String, ByVal IdFactura As String, ByVal Total As String, ByVal Concepto As String) As DataSet
        Dim dts As DataSet = NotaDeDebito.BuscarFiltrado(idNotaDeDebito, Numero, Fecha, IdTalonario, IdFactura, Total, Concepto)
        dts.Tables(0).Columns.Add("IdComprobante")
        Dim i As Integer
        For i = 0 To dts.Tables(0).Rows.Count - 1
            dts.Tables(0).Rows(i)("IdComprobante") = dts.Tables(0).Rows(i)("IdNotaDeDebito").ToString()
        Next
        Return dts
    End Function
    Public Function ObtenerNumero(ByVal CodigoCategoria As String) As Integer
        Dim dts As DataSet = NotaDeDebito.ObtenerNumero(CodigoCategoria)
        If dts.Tables(0).Rows.Count() > 0 Then
            Return CInt(dts.Tables(0).Rows(0)(0).ToString()) + 1
        Else
            Return 1
        End If
    End Function
    Public Function PrepararImpresion(ByVal IdFactura As String, ByVal Concepto As String, ByVal Total As String, ByVal ND As String) As DataSet
        Dim DtsNotaDeDebito As DataSet = New DataSet()
        DtsNotaDeDebito.Tables.Add("TblImpresionFactura")
        DtsNotaDeDebito.Tables(0).Columns.Add("Dia")
        DtsNotaDeDebito.Tables(0).Columns.Add("Mes")
        DtsNotaDeDebito.Tables(0).Columns.Add("Ano")
        DtsNotaDeDebito.Tables(0).Columns.Add("Nombre")
        DtsNotaDeDebito.Tables(0).Columns.Add("Telefono")
        DtsNotaDeDebito.Tables(0).Columns.Add("Domicilio")
        DtsNotaDeDebito.Tables(0).Columns.Add("Localidad")
        DtsNotaDeDebito.Tables(0).Columns.Add("CondicionAnteIVA")
        DtsNotaDeDebito.Tables(0).Columns.Add("CUIT")
        DtsNotaDeDebito.Tables(0).Columns.Add("Contado")
        DtsNotaDeDebito.Tables(0).Columns.Add("ConsumidorFinal")
        DtsNotaDeDebito.Tables(0).Columns.Add("Exento")
        DtsNotaDeDebito.Tables(0).Columns.Add("Monotributo")
        DtsNotaDeDebito.Tables(0).Columns.Add("ResponsableInscripto")
        DtsNotaDeDebito.Tables(0).Columns.Add("CuentaCorriente")
        DtsNotaDeDebito.Tables(0).Columns.Add("Cantidad")
        DtsNotaDeDebito.Tables(0).Columns.Add("Descripcion")
        DtsNotaDeDebito.Tables(0).Columns.Add("PrecioUnitario")
        DtsNotaDeDebito.Tables(0).Columns.Add("PrecioTotal")
        DtsNotaDeDebito.Tables(0).Columns.Add("SubTotal")
        DtsNotaDeDebito.Tables(0).Columns.Add("IVA")
        DtsNotaDeDebito.Tables(0).Columns.Add("Total")
        DtsNotaDeDebito.Tables(0).Columns.Add("PorcentajeIVA")

        Dim DtsFacturaTemp As DataSet = Factura.BuscarFiltrado(IdFactura, "", "", "", "", "", "", "", "", "")
        Dim DtsClienteTemp As DataSet = Cliente.Listar(DtsFacturaTemp.Tables(0).Rows(0)("CodigoCliente").ToString, "", "", "", "", "", "", "", "", "", "", "")
        Dim DtsLocalidadTemp As DataSet = Localidad.Buscar(DtsClienteTemp.Tables(0).Rows(0)("CodigoLocalidad").ToString)
        Dim DtsDetalleFactura As DataSet = DetalleFactura.BuscarFiltrado("", "", "", "", IdFactura, "", "")
        Dim DtsNotaDeCreditoTemp As DataSet = NotaDeDebito.BuscarFiltrado("", ND, "", "", IdFactura, "", "")

        Dim dr As DataRow
        dr = DtsNotaDeDebito.Tables(0).NewRow()
        dr("Dia") = Convert.ToDateTime(DtsNotaDeCreditoTemp.Tables(0).Rows(0)("Fecha")).Day
        dr("Mes") = Convert.ToDateTime(DtsNotaDeCreditoTemp.Tables(0).Rows(0)("Fecha")).Month
        dr("Ano") = Convert.ToDateTime(DtsNotaDeCreditoTemp.Tables(0).Rows(0)("Fecha")).Year
        dr("Nombre") = DtsClienteTemp.Tables(0).Rows(0)("RazonSocial").ToString
        dr("Telefono") = DtsClienteTemp.Tables(0).Rows(0)("TelCel").ToString
        dr("Domicilio") = DtsClienteTemp.Tables(0).Rows(0)("Direccion").ToString
        dr("CUIT") = DtsClienteTemp.Tables(0).Rows(0)("CUIT").ToString
        dr("Localidad") = DtsLocalidadTemp.Tables(0).Rows(0)("Nombre").ToString

        If DtsClienteTemp.Tables(0).Rows(0)("CodigoCategoria").ToString = "1" Then
            dr("ResponsableInscripto") = "X"
            dr("PrecioTotal") = (Math.Round(CDbl(Total) / 1.21)).ToString()
            dr("PrecioUnitario") = (Math.Round(CDbl(Total) / 1.21)).ToString()
            dr("SubTotal") = (Math.Round(CDbl(Total) / 1.21)).ToString()
            dr("IVA") = (Math.Round(CDbl(Total) - (CDbl(Total) / 1.21))).ToString()
            dr("PorcentajeIVA") = "21"
        Else
            dr("ResponsableInscripto") = ""
            dr("PrecioUnitario") = Total
            dr("PrecioTotal") = Total
            dr("SubTotal") = ""
            dr("IVA") = ""
            dr("PorcentajeIVA") = ""
        End If
        If DtsClienteTemp.Tables(0).Rows(0)("CodigoCategoria").ToString = "2" Then
            dr("Monotributo") = "X"
        Else
            dr("Monotributo") = ""
        End If
        If DtsClienteTemp.Tables(0).Rows(0)("CodigoCategoria").ToString = "3" Then
            dr("ConsumidorFinal") = "X"
        Else
            dr("ConsumidorFinal") = ""
        End If
        If DtsClienteTemp.Tables(0).Rows(0)("CodigoCategoria").ToString = "4" Then
            dr("Exento") = "X"
        Else
            dr("Exento") = ""
        End If
        dr("CondicionAnteIVA") = ""
        dr("Contado") = ""
        dr("CuentaCorriente") = "X"

        dr("Cantidad") = "1"
        dr("Descripcion") = "ND :" + Concepto + ". FC:" + DtsFacturaTemp.Tables(0).Rows(0)("Numero").ToString

        dr("Total") = Total
        DtsNotaDeDebito.Tables(0).Rows.Add(dr)
        Return DtsNotaDeDebito
    End Function

End Class
